package com.iplant.wms.service.po.brm;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.po.DBBasePO;
import com.iplant.base.utils.annotation.field.DBField;
import com.iplant.wms.service.basicenum.brm.BRMSerialItemTypes;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;


/**
 * 序列号规则
 *
 * @author ShrisJava
 */
@DBBasePO(TableName = "brm_serialnorule",  CheckName = {"Code","Name"},
        Schemas = DBSchemas.WMS,CodeAuto = true,CodePrefix = "BSNR-")
public class BRMSerialNoRule extends BasePO implements Serializable {
    private static final long serialVersionUID = 1L;



    /**
     * 规则字符串  暂时仅支持前缀固定 生成后续
     */
    @DBField
    public String RuleString = "";

    /**
     * 序列号长度
     */
    @DBField
    public int SNLength = 7;


    /**
     * 最后一次创建编码  序列化只与时间相关 与其他无关
     */
    @DBField
    public String LastBarCode = "";


    /**
     * 规则明细  一个规则明细内部只能包含一个序列号
     */
    @DBField(DefaultValue = "'[]'", CheckValue = true,isJson = true)
    public List<BRMSerialNoItem> RuleItemList = new ArrayList<>();


    public void CodeRuleString(Calendar wProductDate, String wMaterialNo, String wFactoryCode, String wCustomerCode) {
        if (RuleItemList == null || RuleItemList.size() == 0)
            return;

        List<BRMSerialNoItem> wBCMRuleItemList = new ArrayList<>();

        RuleItemList.sort(Comparator.comparingInt(o -> o.SequenceNo));
        RuleString = "";
        boolean isUsed = false;
        for (BRMSerialNoItem wBCMRuleItem : RuleItemList) {
            if (wBCMRuleItem == null)
                continue;

            if (wBCMRuleItem.RuleItemType == BRMSerialItemTypes.Sequence.getValue()) {
                if (isUsed)
                    continue;
                isUsed = true;
            }

            RuleString += wBCMRuleItem.getRuleCodeString(wProductDate, wMaterialNo, wFactoryCode,wCustomerCode);

            wBCMRuleItemList.add(wBCMRuleItem);
        }
        RuleItemList.clear();
        RuleItemList.addAll(wBCMRuleItemList);
    }



    @JsonProperty("RuleString")
    public String getRuleString() {
        return RuleString;
    }

    public void setRuleString(String ruleString) {
        RuleString = ruleString;
    }

    @JsonProperty("SNLength")
    public int getSNLength() {
        return SNLength;
    }

    public void setSNLength(int SNLength) {
        this.SNLength = SNLength;
    }

    @JsonProperty("LastBarCode")
    public String getLastBarCode() {
        return LastBarCode;
    }

    public void setLastBarCode(String lastBarCode) {
        LastBarCode = lastBarCode;
    }



    @JsonProperty("RuleItemList")
    public List<BRMSerialNoItem> getRuleItemList() {
        return RuleItemList;
    }

    public void setRuleItemList(List<BRMSerialNoItem> ruleItemList) {
        RuleItemList = ruleItemList;
    }
}
